Tests in Firminy

Sheng CHEN

First created on 2021-09-04. Updated on 2022-01-03

load data.json

today = "2021-12-22"
cat("data on:", today, "\n")
## data on: 2021-12-22
if (!dir.exists(paste0("data/", today))){
  dir.create(paste0("data/", today))
}

json_data = fromJSON(file = paste0("data/", today, "/position.json"))
cat("Total collected positions: ", length(json_data), "\n")
## Total collected positions:  1392184
tagId_seq = unlist(lapply(json_data, function(x){x["tag_id"][[1]]}))
tagId = unique(tagId_seq)
nb_tag = length(tagId)

cat(nb_tag, "Tags are: ", tagId, "\n")
## 35 Tags are:  19ab 4584 36fb 4596 11d2 436d 38d7 4f2e 373f 44c5 2f7b 424f 468c 2f40 67cf 44e0 1a10 4585 2b9c 460b 2e56 1a04 1a0a 4288 14bc 4258 3722 4592 3906 cf31 4273 3719 181b 3721 4586
table(tagId_seq)
## tagId_seq
##   11d2   14bc   181b   19ab   1a04   1a0a   1a10   2b9c   2e56   2f40   2f7b 
##  52340  49092   1703 420702  34141  29903   6037   5468   6454   3503   5630 
##   36fb   3719   3721   3722   373f   38d7   3906   424f   4258   4273   4288 
## 111169   1626   2330   9572  11516  79929   2878  12155   8281   1616  29771 
##   436d   44c5   44e0   4584   4585   4586   4592   4596   460b   468c   4f2e 
## 104537  10102   8691 115222   5224   2108  18960  80654   3107 111273  42153 
##   67cf   cf31 
##   4240     97
names_tag <- read.xlsx("Z/Inventaire Tags - Firminy - 2022.xlsx")
names_tag = names_tag %>% 
  filter(Identifiant %in% tagId) 
names_tag = names_tag[order(match(names_tag$Identifiant, tagId)),]

general analysis

dat <- data.frame(tag = unlist(lapply(json_data, function(x){x["tag_id"][[1]]})),
                  x = unlist(lapply(json_data, function(x){x["x"][[1]]})),
                  y = unlist(lapply(json_data, function(x){x["y"][[1]]})),
                  record_timestamp = unlist(lapply(json_data, function(x){x["record_timestamp"][[1]]})))
dat = dat[order(dat$record_timestamp),]
dat = cbind.data.frame(dat, convert_date(dat$record_timestamp))
dat$x = as.numeric(dat$x)
dat$y = as.numeric(dat$y)

list_tag <- split(dat, dat$tag)

quality of collecting data

table_tag <- data.frame(tag = names_tag$Identifiant, type = names_tag$Type.de.tag)
table_tag$first_record = NA
table_tag$last_record = NA
table_tag$number = NA
table_tag$number_NA = NA
table_tag$ratio_non_NA = NA
table_tag$freq_1Q = NA
table_tag$freq_median = NA
table_tag$freq_3Q = NA

for (k in 1:nb_tag){
  tag = table_tag$tag[k]
  temp = list_tag[tag][[1]]
  temp$diff_ts = c(0, temp$record_timestamp[-1]-temp$record_timestamp[-nrow(temp)])
  
  table_tag$first_record[k] = head(as.character(temp$date),1)
  table_tag$last_record[k] = tail(as.character(temp$date),1)
  table_tag$number[k] = nrow(temp)
  table_tag$number_NA[k] = sum(is.na(temp$x))
  table_tag$ratio_non_NA[k] = round(1-table_tag$number_NA[k]/table_tag$number[k],2)
  table_tag$freq_1Q[k] = round(quantile(temp$diff_ts, 0.25), 3)
  table_tag$freq_median[k] = round(quantile(temp$diff_ts, 0.5), 3)
  table_tag$freq_3Q[k] = round(quantile(temp$diff_ts, 0.75), 3)
}

kable(table_tag) %>%
  kable_styling(bootstrap_options = "striped", full_width = F)
tag type first_record last_record number number_NA ratio_non_NA freq_1Q freq_median freq_3Q
19ab Tag fixe 2021-12-22 00:00:00 2021-12-22 23:59:59 420702 151707 0.64 0.200 0.2 0.200
4584 Infirmiere 2021-12-22 09:27:33 2021-12-22 23:59:59 115222 21976 0.81 0.200 0.2 0.200
36fb Infirmiere 2021-12-22 09:27:34 2021-12-22 23:59:10 111169 13261 0.88 0.200 0.2 0.200
4596 Medecin 2021-12-22 09:27:33 2021-12-22 23:58:34 80654 12301 0.85 0.200 0.2 0.200
11d2 Aide soignant 2021-12-22 09:27:34 2021-12-22 23:58:02 52340 7551 0.86 0.200 0.2 0.200
436d Medecin 2021-12-22 09:27:33 2021-12-22 23:57:45 104537 15696 0.85 0.200 0.2 0.200
38d7 Infirmiere 2021-12-22 09:27:33 2021-12-22 23:54:40 79929 14755 0.82 0.200 0.2 0.200
4f2e Infirmiere 2021-12-22 09:27:34 2021-12-22 23:42:17 42153 12536 0.70 0.200 0.2 0.201
373f Materiel 2021-12-22 09:27:33 2021-12-22 23:29:24 11516 982 0.91 0.200 0.2 0.200
44c5 Materiel 2021-12-22 09:27:34 2021-12-22 23:24:51 10102 1366 0.86 0.200 0.2 0.200
2f7b Brancard2 2021-12-22 00:00:25 2021-12-22 23:16:19 5630 143 0.97 0.200 0.2 0.200
424f Brancardier 2021-12-22 09:27:33 2021-12-22 23:03:33 12155 3054 0.75 0.200 0.2 0.200
468c Infirmiere 2021-12-22 09:27:33 2021-12-22 22:11:36 111273 11905 0.89 0.200 0.2 0.200
2f40 Brancard4 2021-12-22 00:37:54 2021-12-22 21:28:22 3503 276 0.92 0.200 0.2 0.200
67cf Agent d’accueil 2021-12-22 09:27:34 2021-12-22 21:22:11 4240 1271 0.70 0.200 0.2 0.201
44e0 Materiel 2021-12-22 09:27:33 2021-12-22 20:59:00 8691 1123 0.87 0.200 0.2 0.200
1a10 Materiel 2021-12-22 09:27:33 2021-12-22 20:06:26 6037 1172 0.81 0.200 0.2 0.200
4585 Brancardier 2021-12-22 09:27:31 2021-12-22 19:43:04 5224 2268 0.57 0.200 0.2 0.200
2b9c Brancard polytrauma2 2021-12-22 11:18:34 2021-12-22 19:42:49 5468 959 0.82 0.200 0.2 0.200
460b Agent d’accueil 2021-12-22 09:27:33 2021-12-22 19:31:00 3107 1119 0.64 0.199 0.2 0.201
2e56 Agent de service hospitalier 2021-12-22 09:27:33 2021-12-22 19:31:00 6454 2299 0.64 0.200 0.2 0.201
1a04 Aide soignant 2021-12-22 09:27:33 2021-12-22 19:22:46 34141 2963 0.91 0.200 0.2 0.201
1a0a Aide soignant 2021-12-22 09:27:33 2021-12-22 19:22:28 29903 4591 0.85 0.200 0.2 0.201
4288 Brancardier 2021-12-22 09:27:35 2021-12-22 19:19:55 29771 5145 0.83 0.200 0.2 0.200
14bc Medecin 2021-12-22 09:27:33 2021-12-22 18:06:34 49092 12230 0.75 0.200 0.2 0.200
4258 Agent de service hospitalier 2021-12-22 09:27:34 2021-12-22 17:15:52 8281 1425 0.83 0.200 0.2 0.200
3722 Medecin 2021-12-22 09:27:33 2021-12-22 13:02:03 9572 3513 0.63 0.200 0.2 0.200
4592 Infirmiere 2021-12-22 09:27:33 2021-12-22 12:36:16 18960 3876 0.80 0.200 0.2 0.200
3906 Materiel 2021-12-22 09:27:34 2021-12-22 12:25:32 2878 904 0.69 0.199 0.2 0.201
cf31 tag_IDE 2021-12-22 10:40:54 2021-12-22 11:13:15 97 97 0.00 0.001 1.0 1.000
4273 Medecin 2021-12-22 09:27:34 2021-12-22 10:57:01 1616 939 0.42 0.199 0.2 0.202
3719 Medecin 2021-12-22 09:27:34 2021-12-22 10:57:01 1626 1081 0.34 0.199 0.2 0.201
181b Medecin 2021-12-22 09:27:32 2021-12-22 10:57:01 1703 1345 0.21 0.199 0.2 0.201
3721 Nominatif 2021-12-22 09:27:33 2021-12-22 10:40:51 2330 1638 0.30 0.199 0.2 0.201
4586 Nominatif 2021-12-22 09:27:34 2021-12-22 10:40:51 2108 1405 0.33 0.199 0.2 0.201
x_na = which(is.na(dat$x))
y_na = which(is.na(dat$y))
cat("if x_na = y_na:", identical(x_na, y_na), "\n")
## if x_na = y_na: TRUE
cat("number of invalid positions:", length(x_na), "/", length(tagId_seq), "(=", 
    length(x_na)/length(tagId_seq)*100, "%)", "\n")
## number of invalid positions: 318872 / 1392184 (= 22.90444 %)
## create position.csv
# list_tag <- split(dat, dat$tag)

load("Plan/plan_firminy.RData")

for (tag in names(list_tag)){
  if (!is.null(list_tag[tag][[1]])){
    type = names_tag$Type.de.tag[names_tag$Identifiant==tag]
    dd = list_tag[tag][[1]]
    dd[,c("x","y")] = dd[,c("x","y")]/100
    rownames(dd) = 1:nrow(dd)
    dd$num = 1:nrow(dd)
    dd$timediff = c(0, dd$record_timestamp[-1] - dd$record_timestamp[-nrow(dd)])
    # dd$date = dd$record_timestamp
    # class(dd$date) = c('POSIXt','POSIXct')
    # dd$date = with_tz(dd$date, "CET")
    # dd$hour = format(dd$date, "%H")
    
    if (sum(!is.na(dd$x))!=0){
      q <- p + 
        geom_point(data = dd, aes(x=x,y=y), col = "red", size = 0.5) + 
        coord_equal(ratio = 1, xlim = c(-20,17), ylim = c(-35,35)) +
        labs(title = today, subtitle = paste(tag, " - ", type, " - ", sum(!is.na(dd$x))), x = "", y = "")
      print(q)
      ggsave(paste0("data/", today, "/fig/", tag, ".png"), width=6, height=11)
    }
  
    list_tag[tag][[1]] = dd
    write.csv2(dd, file = paste0("data/", today, "/", tag, ".csv"), row.names = F)
  }
}
## Warning: Removed 7551 rows containing missing values (geom_point).

## Warning: Removed 7551 rows containing missing values (geom_point).

## Warning: Removed 12230 rows containing missing values (geom_point).
## Warning: Removed 12230 rows containing missing values (geom_point).

## Warning: Removed 1345 rows containing missing values (geom_point).
## Warning: Removed 1345 rows containing missing values (geom_point).

## Warning: Removed 151707 rows containing missing values (geom_point).
## Warning: Removed 151707 rows containing missing values (geom_point).

## Warning: Removed 2963 rows containing missing values (geom_point).
## Warning: Removed 2963 rows containing missing values (geom_point).

## Warning: Removed 4591 rows containing missing values (geom_point).
## Warning: Removed 4591 rows containing missing values (geom_point).

## Warning: Removed 1172 rows containing missing values (geom_point).
## Warning: Removed 1172 rows containing missing values (geom_point).

## Warning: Removed 959 rows containing missing values (geom_point).
## Warning: Removed 959 rows containing missing values (geom_point).

## Warning: Removed 2299 rows containing missing values (geom_point).
## Warning: Removed 2299 rows containing missing values (geom_point).

## Warning: Removed 276 rows containing missing values (geom_point).
## Warning: Removed 276 rows containing missing values (geom_point).

## Warning: Removed 143 rows containing missing values (geom_point).
## Warning: Removed 143 rows containing missing values (geom_point).

## Warning: Removed 13261 rows containing missing values (geom_point).
## Warning: Removed 13261 rows containing missing values (geom_point).

## Warning: Removed 1081 rows containing missing values (geom_point).
## Warning: Removed 1081 rows containing missing values (geom_point).

## Warning: Removed 1638 rows containing missing values (geom_point).
## Warning: Removed 1638 rows containing missing values (geom_point).

## Warning: Removed 3513 rows containing missing values (geom_point).
## Warning: Removed 3513 rows containing missing values (geom_point).

## Warning: Removed 982 rows containing missing values (geom_point).
## Warning: Removed 982 rows containing missing values (geom_point).

## Warning: Removed 14755 rows containing missing values (geom_point).
## Warning: Removed 14755 rows containing missing values (geom_point).

## Warning: Removed 904 rows containing missing values (geom_point).
## Warning: Removed 904 rows containing missing values (geom_point).

## Warning: Removed 3054 rows containing missing values (geom_point).
## Warning: Removed 3054 rows containing missing values (geom_point).

## Warning: Removed 1425 rows containing missing values (geom_point).
## Warning: Removed 1425 rows containing missing values (geom_point).

## Warning: Removed 939 rows containing missing values (geom_point).
## Warning: Removed 939 rows containing missing values (geom_point).

## Warning: Removed 5145 rows containing missing values (geom_point).
## Warning: Removed 5145 rows containing missing values (geom_point).

## Warning: Removed 15696 rows containing missing values (geom_point).
## Warning: Removed 15696 rows containing missing values (geom_point).

## Warning: Removed 1366 rows containing missing values (geom_point).
## Warning: Removed 1366 rows containing missing values (geom_point).

## Warning: Removed 1123 rows containing missing values (geom_point).
## Warning: Removed 1123 rows containing missing values (geom_point).

## Warning: Removed 21976 rows containing missing values (geom_point).
## Warning: Removed 21976 rows containing missing values (geom_point).

## Warning: Removed 2268 rows containing missing values (geom_point).
## Warning: Removed 2268 rows containing missing values (geom_point).

## Warning: Removed 1405 rows containing missing values (geom_point).
## Warning: Removed 1405 rows containing missing values (geom_point).

## Warning: Removed 3876 rows containing missing values (geom_point).
## Warning: Removed 3876 rows containing missing values (geom_point).

## Warning: Removed 12301 rows containing missing values (geom_point).
## Warning: Removed 12301 rows containing missing values (geom_point).

## Warning: Removed 1119 rows containing missing values (geom_point).
## Warning: Removed 1119 rows containing missing values (geom_point).

## Warning: Removed 11905 rows containing missing values (geom_point).
## Warning: Removed 11905 rows containing missing values (geom_point).

## Warning: Removed 12536 rows containing missing values (geom_point).
## Warning: Removed 12536 rows containing missing values (geom_point).

## Warning: Removed 1271 rows containing missing values (geom_point).
## Warning: Removed 1271 rows containing missing values (geom_point).

dat = do.call(rbind.data.frame, list_tag)
write.csv2(dat, file = paste0("data/", today, "/position.csv"), row.names = F)